<?php
// lib/validator/myValidatedFile.php
class myValidatedFile extends sfValidatedFile
{
  function replace_accents($string) 
  { 
	  $stripped = preg_replace('`&[^;]+;`','',htmlentities($string));
	  $stripped = str_replace(" ", "-", $stripped);
	  return $stripped;
  } 

  public function generateFilename()
  {
    return $this->path . DIRECTORY_SEPARATOR . $this->replace_accents($this->getOriginalName());
  }

  public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777)
  {
    $result = parent::save($file, $fileMode, $create, $dirMode);
	//Extraemos el productId del path pasado por parametro
	$productId = explode('/', $this->path);
	$productoId = end($productId);
	//Creamos el thumbnail a partir del sfImageTransformPlugin
	$img = new sfImage($this->generateFilename(), 'image/jpg');
	$img->setQuality(90);
	$img->resize(sfConfig::get('app_width_thumb_pic'), sfConfig::get('app_height_thumb_pic'));
	$img->saveAs(sfConfig::get('sf_upload_dir').'/assets/thumbs/'.$productoId.'/'.$this->replace_accents($this->getOriginalName()));
	
    // Alter the savedName to reflect what we're after
    $this->savedName = str_replace("", "", $this->savedName);

    return null === $this->path ? $this->savedName : str_replace($this->path.DIRECTORY_SEPARATOR, '', $this->savedName);
  }
}